package com.kuiandjie.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.kuiandjie.entity.Contact;
import com.kuiandjie.util.DBHelp;
import com.kuiandjie.util.RowMapper;

public class ContactDao {
	private DBHelp<Contact> db = new DBHelp<Contact>();
	
	private class ContactMap implements RowMapper<Contact>{
//test2
		@Override
		public Contact mapperRow(ResultSet rs) throws SQLException {
			Contact contact = new Contact();
			
			contact.setId(rs.getInt("id"));
			contact.setAddress(rs.getString("address"));
			contact.setCompanyname(rs.getString("companyname"));
			contact.setContent(rs.getString("content"));
			contact.setEmail(rs.getString("email"));
			contact.setMoblie(rs.getString("moblie"));
			contact.setName(rs.getString("name"));
			contact.setProjectid(rs.getInt("projectid"));
			contact.setTel(rs.getString("tel"));
			contact.setWebsite(rs.getString("website"));
			contact.setWeibo(rs.getString("weibo"));
			
			return contact;
		}
	}

	public List<Contact> getContactByProId(int id) {
		String sql = "SELECT id, address, companyname, content, email, moblie, name, projectid, tel, website, weibo FROM t_contact WHERE projectid = ?";
		return db.executeQueryForList(sql, new ContactMap(), id);
	}

	public int setContact(Contact contact) {
		String sql = "INSERT INTO t_contact (name, companyname, moblie, tel, email, address, website, weibo, content, projectid, createtime) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		return db.getAutoKey(sql, contact.getName(), contact.getCompanyname(), contact.getMoblie(), contact.getTel(), contact.getEmail(), contact.getAddress(), contact.getWebsite(), contact.getWeibo(), contact.getContent(), contact.getProjectid(), contact.getCreatetime());
	}

	public Contact getContactById(int id) {
		String sql = "SELECT id, address, companyname, content, email, moblie, name, projectid, tel, website, weibo FROM t_contact WHERE id = ?";
		return db.executeQueryForObject(sql, new ContactMap(), id);
	}

	public Contact getContactByIdAndProId(int id, int id2) {
		String sql = "SELECT id, address, companyname, content, email, moblie, name, projectid, tel, website, weibo FROM t_contact WHERE id = ? AND projectid = ?";
		return db.executeQueryForObject(sql, new ContactMap(), id, id2);
	}

	public void updateContact(Contact c) {
		String sql = "UPDATE t_contact SET address = ?, companyname = ?, content = ?, email = ?, moblie = ?, name = ?, tel = ?, website = ?, weibo = ? WHERE id = ?";
		db.executeSQL(sql, c.getAddress(), c.getCompanyname(), c.getContent(), c.getEmail(), c.getMoblie(), c.getName(), c.getTel(), c.getWebsite(), c.getWeibo(), c.getId());
	}

	public void deleteById(int id) {
		String sql = "DELETE FROM t_contact WHERE id = ?";
		db.executeSQL(sql, id);
	}
	
	
}
